<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class CacheClear extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'redis:clear {ids*} {--type=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'clear face or user-info or spider cache';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Description:
     * Author: Sam
     * DateTime: 2019-03-07 16:26
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function handle()
    {
        $ids = $this->argument('ids');
        $type = $this->option('type');
        $all = count($ids)==1&&$ids[0]=='all'?true:false;
        if(empty($type)){
            $this->error('type can not be null!');
        }
        switch ($type){
            case 'info':
                if($all){
                    $redis = Cache::getRedis();
                    $keys = $redis->keys("*user_info_*");
                    foreach ($keys as $key){
                        $redis->del($key);
                    }
                }else{
                    foreach ($ids as $id){
                        Cache::delete('user_info_'.$id);
                    }
                }
                break;
            case 'face':
                if($all){
                    $redis = Cache::getRedis();
                    $keys = $redis->keys("*face_cache_*");
                    foreach ($keys as $key){
                        $redis->del($key);
                    }
                }else{
                    foreach ($ids as $id){
                        Cache::delete('face_cache_'.$id);
                    }
                }
                break;
            case 'spider':
                if($all){
                    $redis = Cache::getRedis();
                    $keys = $redis->keys("*spider:1000_0002*");
                    foreach ($keys as $key){
                        $redis->del($key);
                    }
                }else{
                    $redis = Cache::getRedis();
                    foreach ($ids as $id){
                        $redis->del('spider:1000_0002_1_'.$id);
                        $redis->del('spider:1000_0002_2_'.$id);
                    }
                }
                break;
            case 'all':
                $redis = Cache::getRedis();
                $keys_info = $redis->keys("*user_info_*");
                $keys_face = $redis->keys("*face_cache_*");
                $keys_spider = $redis->keys("*spider:1000_0002*");
                foreach ($keys_info as $key){
                    $redis->del($key);
                }
                foreach ($keys_face as $key){
                    $redis->del($key);
                }
                foreach ($keys_spider as $key){
                    $redis->del($key);
                }
                break;
        }
    }
}
